/***********************************************
 **** 版权声明处 **
 ****  为了方便阅读和维护，请严格遵守相关代码规范，谢谢   ****
 *******************************************/
package com.hnisi.decorators
{
	import com.hnisi.Link;
	
	import flash.display.Sprite;
	
	import mx.collections.ArrayCollection;
	
	/*******************************************
	 **** @author huangjixin,@date 2012-6-21, @time 下午3:20:26 **
	 **** 排列边饰器  **
	 *******************************************/
	public class AlignmentDecorator implements IDecorator
	{
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 类私有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：private static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 类公有静态变量和静态常量声明处。（全部大写，使用下划线进行分割）
		// 例如：public static const EXAMPLE:String = "example";
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// private 私有变量声明处，请以“_”开头定义变量
		// 例如：private var _example:String;
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// public 公有变量声明处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 构造函数，初始化相关工作可以放在里面
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		public function AlignmentDecorator()
		{
		}
		
		/**
		 * 更新選擇對象； 
		 * @param allObject
		 * @param selectedObjects
		 * @param drawingLayer
		 * 
		 */
		public function updateSel(allObject:ArrayCollection, selectedObjects:ArrayCollection, drawingLayer:Sprite):void
		{
			drawingLayer.graphics.clear();
		}
		
		/**
		 * 更新绘制信息； 
		 * @param allObject
		 * @param selectedObjects
		 * @param movedObjects
		 * @param drawingLayer
		 * 
		 */
		public function updatePos(allObject:ArrayCollection, selectedObjects:ArrayCollection, drawingLayer:Sprite):void
		{
			//仅对一个
			if( selectedObjects.length != 1 ) 
				return;
			var o:Object = selectedObjects[0];
			drawingLayer.graphics.clear();
			drawingLayer.graphics.lineStyle(1,0x065CB3,1);
			
			for each ( var other:Object in allObject )
			{
				if( other == o ) continue;
				if(other is Link){
					continue;
				}
				if( Math.abs(o.x-other.x) < 3 ) 
					drawVerticalLine(other.x,drawingLayer);
				if( Math.abs(o.y-other.y) < 3 ) 
					drawHorizontalLine(other.y,drawingLayer);
				if( Math.abs((o.x+o.width)-(other.x+other.width)) < 3 ) 
					drawVerticalLine(other.x+other.width,drawingLayer);
				if( Math.abs((o.y+o.height)-(other.y + other.height)) < 3 ) 
					drawHorizontalLine(other.y+other.height,drawingLayer);
				
				if( Math.abs(o.x-(other.width+other.x)) < 3 ) 
					drawVerticalLine(o.x,drawingLayer);
				if( Math.abs(o.y-(other.y+other.height)) < 3 ) 
					drawHorizontalLine(o.y,drawingLayer);
				if( Math.abs((o.x+o.width)-other.x) < 3 ) 
					drawVerticalLine(o.x+o.width,drawingLayer);
				if( Math.abs((o.y+o.height)-other.y) < 3 ) 
					drawHorizontalLine(o.y+o.height,drawingLayer);
				
				/*if( Math.abs((o.x+o.width/2)-(other.x+other.width/2)) < 3 ) 
					drawVerticalLine(other.x+other.width/2,drawingLayer);
				if( Math.abs((o.y+o.height/2)-(other.y + other.height/2)) < 3 ) 
					drawHorizontalLine(other.y+other.height/2,drawingLayer);*/
			}
		}
		
		/**
		 * 对拖拽的节点进行位置进行调整。 
		 * @param allObject
		 * @param selectedObjects
		 * 
		 */
		public function adjustPos( allObject:ArrayCollection, selectedObjects:ArrayCollection):void{
			//仅对一个
			if( selectedObjects.length != 1 ) 
				return;
			var o:Object = selectedObjects[0];
			
			for each ( var other:Object in allObject )
			{
				if( other == o ) 
					continue;
				if(other is Link){
					continue;
				}
				if( Math.abs(o.x-other.x) < 3 ){
					o.x = other.x;
//					break;
				} 
					
				if( Math.abs(o.y-other.y) < 3 ){
					o.y = other.y;
//					break;
				} 
					 
				if( Math.abs((o.x+o.width)-(other.x+other.width)) < 3 ){
					o.x = other.x+other.width - o.width;
					//					break;
				} 
				
				if( Math.abs((o.y+o.height)-(other.y + other.height)) < 3 ){
					o.y = other.y+other.height - o.height;
					//					break;
				} 
				
				/*if( Math.abs((o.x+o.width/2)-(other.x+other.width/2)) < 3 ){
					o.x = other.x+other.width/2 - o.width/2;
					//					break;
				} 
				
				if( Math.abs((o.y+o.height/2)-(other.y + other.height/2)) < 3 ){
					o.y = other.y+other.height/2 - o.height/2;
					//					break;
				} */
					
				
				if( Math.abs(o.x-(other.width+other.x)) < 3 ){
					o.x = other.width+other.x;
//					break;
				} 
					
				if( Math.abs(o.y-(other.y+other.height)) < 3 ){
					o.y = (other.y+other.height);
//					break;
				} 
					
				if( Math.abs((o.x+o.width)-other.x) < 3 ){
					o.x = other.x - o.width;
//					break;
				} 
					
				if( Math.abs((o.y+o.height)-other.y) < 3 ) {
					o.y =  other.y - o.height;
//					break;
				}
				
			}
		}
		
		/**
		 * 绘制竖线； 
		 * @param x
		 * @param sprite
		 * 
		 */
		protected function drawVerticalLine( x:Number, sprite:Sprite ) : void
		{
			sprite.graphics.moveTo(x,0);
			sprite.graphics.lineTo(x,3000);
		}
		
		/**
		 * 绘制横线； 
		 * @param y
		 * @param sprite
		 * 
		 */
		protected function drawHorizontalLine( y:Number, sprite:Sprite ) : void
		{
			sprite.graphics.moveTo(0,y);
			sprite.graphics.lineTo(3000,y);
		}
		
		/**
		 * 清除绘制的线； 
		 * @param drawingLayer
		 * 
		 */
		public function cleanup(drawingLayer:Sprite):void
		{
			drawingLayer.graphics.clear();
		}//构造函数结束
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// getter和setter函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// 相关事件响应函数和逻辑函数存放处
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		
		
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
		// override 覆盖函数
		//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
	}//类结束
}//包结束